Step 1: Compute linear predictor z:
Step 2: Apply sigmoid:
Step 3: Classify with threshold 0.5:
0.881 > 0.5 → Predicted class = 1 (positive).
Linear regression predicts unbounded real numbers — perfect for house prices or temperature, but catastrophically wrong for classification, where we want a valid probability in the range [0, 1]. This unit introduces Logistic Regression, the workhorse of binary classification. Despite the name, it is a classification model: it squashes a linear predictor through the Sigmoid function to produce calibrated probabilities, then thresholds them to make class predictions. We derive the Binary Cross-Entropy (log-loss) cost function from maximum-likelihood principles, derive the elegant gradient-descent update rules, and work a full numerical example.
Task: Predict whether an Iris flower is Virginica (Y = 1) or not (Y = 0) from petal length + width. We try ordinary linear regression and get outputs like these:
We need a model that always outputs values in \( (0, 1) \) and has a probabilistic foundation. That model is logistic regression.
Logistic regression is used for classification, most commonly binary classification where \( Y \in \{0, 1\} \). We model a transformation of the probability linearly, then invert back.
Let \( p = P(Y = 1 \mid x) \) be the probability of the positive class. The odds of success are:
Take the natural log — now the range is the entire real line, perfect for a linear model:
This is why it's called logistic regression: we perform a linear regression on the log-odds:
Solving for \( p \) by exponentiating both sides and rearranging gives us the Sigmoid (Logistic) function:
The sigmoid "squashes" the unbounded linear output \( z = \theta^T x \) into the valid probability range \( (0, 1) \). Key properties:
Once we have \( p = \sigma(z) \), we apply a threshold function to get a binary prediction \( \hat{y} \):
(The 0.5 threshold can be tuned — raise it to reduce false positives, lower it to reduce false negatives.)
A fitted logistic-regression model for personal-loan acceptance (bank dataset, 5000 customers, only 9.6% accepted the previous campaign). The learned coefficients:
| Feature | Coefficient \( \theta_j \) |
|---|---|
| Intercept (\( \theta_0 \)) | −4.0 |
| Income (\( x_1 \), in $10K units) | +0.8 |
| Has Securities Account (\( x_2 \), 0/1) | +1.2 |
| Age (\( x_3 \), in years) | +0.02 |
Customer: Income = $70K → \( x_1 = 7 \), Has Securities → \( x_2 = 1 \), Age = 45 → \( x_3 = 45 \).
Now apply the sigmoid:
The model estimates a 97.6% chance this customer will accept the loan — confidently predicted class = 1 (Accept).
Binary labels are Bernoulli trials. Each true label follows:
The Bernoulli probability mass function can be written compactly in one line:
Check: if \( y = 1 \), it gives \( p \); if \( y = 0 \), it gives \( 1-p \). ✓
We want to maximize log-likelihood. Equivalently (since optimization code typically minimizes), we define the logistic loss / Binary Cross-Entropy Loss as negative log-likelihood:
where \( h_\theta(x_i) = \sigma(\theta^T x_i) \).
The derivation is worth following once. Despite the sigmoid and the logarithms in the loss, the algebra simplifies to a gradient with exactly the same form as the one we obtained for linear regression. Only the hypothesis \( h \) differs.
Step 1: Derivative of one example's loss w.r.t. parameter \( \theta_j \):
Step 2: Combine fractions, cancel the \( h(1-h) \) denominator:
Step 3: Use the sigmoid derivative identity:
Substitute in — the \( h(1-h) \) factors cancel perfectly:
Average over all m examples to get the full batch gradient:
These look identical in form to Linear Regression — the only difference is the hypothesis \( h \):
Or, compactly using the design matrix \( X \):
Without a calculator, mentally approximate these sigmoid values:
A. \( \sigma(z) \) as \( z \to +\infty \) ?
B. \( \sigma(z) \) as \( z \to -\infty \) ?
C. \( \sigma(0) \) ? What does this mean for a decision threshold at 0.5?
You are building a churn model for a telecom (Churn = Yes/No). The learned coefficients are:
Question: For each feature, does increasing it make churn MORE likely or LESS likely? (Intuition only — no exact numbers needed.)
Business takeaway: Offering free/cheap tech support to at-risk high-charge new customers could reduce churn!
True label y = 1. Model A predicts p = 0.98. Model B predicts p = 0.45. Which has lower cross-entropy loss for this example, and by how much (roughly)?
Model A wins by a mile. Cross-entropy heavily penalizes confident-but-wrong predictions and also uncertain predictions on clear examples.
Logistic model: \( z = -3 + 1.5 x_1 - 0.2 x_2 \). Customer features: \( x_1 = 4 \), \( x_2 = 5 \).
Step 1: Compute linear predictor z:
Step 2: Apply sigmoid:
Step 3: Classify with threshold 0.5:
0.881 > 0.5 → Predicted class = 1 (positive).
Batch of 3 examples. Compute the total average loss \( J(\theta) \).
| i | \( y_i \) | \( p_i = \sigma(z_i) \) | \( y_i \log p_i \) | \( (1-y_i)\log(1-p_i) \) | Sum = loss contribution |
|---|---|---|---|---|---|
| 1 | 1 | 0.90 | log 0.9 ≈ −0.105 | 0 | −0.105 |
| 2 | 0 | 0.60 | 0 | log 0.4 ≈ −0.916 | −0.916 |
| 3 | 1 | 0.30 | log 0.3 ≈ −1.204 | 0 | −1.204 |
Step 1: Sum the last column: −0.105 − 0.916 − 1.204 = −2.225.
Step 2: Apply \( J = -\frac{1}{m} \sum \):
Interpretation: Example 3 (y = 1 but p only 0.30) contributes the most loss — the model confidently got it wrong! That's the point of cross-entropy: it punishes bad calls.
One example (m = 1): \( x_0 = 1,\ x_1 = 2 \). True \( y = 1 \). Current \( \theta_0 = 0 \), \( \theta_1 = 0 \), α = 0.5.
Step 1: Compute z and \( h = \sigma(z) \):
Step 2: Error \( h - y = 0.5 - 1 = -0.5 \).
Step 3: Gradients (divide by m = 1):
Step 4: Apply the updates (θ = θ − α · gradient):
Sanity check: both parameters moved in the positive direction, which increases \( z = \theta^T x \), which increases \( \sigma(z) \), which moves p upward toward the true label y = 1. ✓ (The model was under-confident in the positive class; it corrected itself.)
A logistic regression predicts log-odds \( \ln(p/(1-p)) = -1 \) for a given customer. What is the predicted probability p? What class is predicted at the 0.5 threshold?
Exponentiate: \( p/(1-p) = e^{-1} \approx 0.368 \).
Solve: \( p = 0.368 (1-p) \implies p + 0.368p = 0.368 \implies p = 0.368/1.368 \approx \mathbf{0.269} \).
p ≈ 26.9% < 50% → Predicted class = 0 (negative class).
A student suggests: "Why not just use MSE on top of the sigmoid? That way we don't have to derive all this log stuff." Give two reasons (statistical or optimization-based) why binary cross-entropy is the better choice for logistic regression.
After the update in Problem 3 (Numerical Solutions), you have new parameters \( \theta_0 = 0.25 \), \( \theta_1 = 0.5 \). The same example (\( x_0=1, x_1=2, y=1 \)) is processed again in the next iteration. Compute the NEW gradients \( \partial J/\partial \theta_0 \) and \( \partial J/\partial \theta_1 \).
z = 0.25 + 0.5(2) = 1.25.
h = σ(1.25) = 1 / (1 + e−1.25) ≈ 1 / (1 + 0.287) ≈ 0.777.
Error = h − y = 0.777 − 1.000 = −0.223 (notice it's smaller than the first iteration's −0.5! The model is learning.)
Gradients got smaller — the model is converging toward the correct answer. This is what healthy GD looks like.
Answer all 5 questions. Click an option for instant feedback.
Your score: 0 / 5